/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.animation;

import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * 
 * @author xiumeteo
 */
public class PlaneRunnable {
	ImageIcon avionIcon;
	Image avionImage;
	JButton marcador;
	JPanel panel;
	int ancho = 60;
	int largo = 50;

	public PlaneRunnable(ImageIcon i, JButton j1, JPanel panel) {
		avionIcon = i;
		marcador = j1;
		this.panel = panel;

		panel.setLayout(null);

	}

	public void takeOff() throws InterruptedException {
		//acomodamos el avioncio
		marcador.setLocation(marcador.getX()+50, 0);
		
		while (marcador.getY() < panel.getHeight()) {

			avionImage = avionIcon.getImage();
			largo++;
			ancho++;
			if (largo > 0 && ancho > 0) {
				Image another = avionImage.getScaledInstance(ancho, largo,
						Image.SCALE_AREA_AVERAGING);
				ImageIcon escalado = new ImageIcon(another);
				marcador.setIcon(escalado);
				marcador.setSize(escalado.getIconWidth(),
						escalado.getIconHeight());
			}

			marcador.setLocation(marcador.getX(), marcador.getY() + 10);

			panel.repaint();

			Thread.sleep(50);
		}

		panel.remove(marcador);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel.repaint();
		panel.validate();

	}

	public void land() throws InterruptedException {

		ancho = 90;
		largo = 80;

		int h_original = avionIcon.getIconHeight();
		int w_original = avionIcon.getIconWidth();

		while (marcador.getY() > panel.getY() - 190) {

			avionImage = avionIcon.getImage();
			largo--;
			ancho--;
			if (largo > 0 && ancho > 0) {
				Image another = avionImage.getScaledInstance(ancho, largo,
						Image.SCALE_AREA_AVERAGING);
				ImageIcon escalado = new ImageIcon(another);
				marcador.setIcon(escalado);
				marcador.setSize(escalado.getIconWidth(),
						escalado.getIconHeight());
			}

			marcador.setLocation(marcador.getX(), marcador.getY() + 10);
			panel.repaint();

			Thread.sleep(50);
		}

		panel.remove(marcador);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel.repaint();
		panel.validate();

	}

	public void rotatePlane() throws InterruptedException {

		int angle = 0;

		int offset = 30;

		while (angle <= 90) {

			angle = +offset;

			avionImage = avionIcon.getImage();
			avionIcon = new ImageIcon(ImageHandler.rotatePlane(avionImage,
					angle));
			marcador.setIcon(avionIcon);
			panel.repaint();
			panel.validate();

			Thread.sleep(500);

		}

		panel.remove(marcador);
		panel.repaint();
		panel.validate();

	}

}
